package com.example.spring_config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring_config.dao.PermissionRoleRelationDao;
import com.example.spring_config.mapper.RolePermission;
import com.example.spring_config.service.PermissionRoleRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionRoleRelationServiceImpl extends ServiceImpl<PermissionRoleRelationDao, RolePermission> implements PermissionRoleRelationService{

    @Autowired
    private PermissionRoleRelationDao relationDao;

    /**
     * ；给角色赋予权限
     * @param rid 角色id
     * @param pids 权限集合
     * @return 是否成功
     */
    @Override
    public Boolean addRolePermission(String rid,List<String> pids) {
        // 删除角色原有权限
        System.out.println("====================================");
        pids.forEach(System.out::println);
        System.out.println("====================================");
        remove(new QueryWrapper<RolePermission>().eq("r_id",rid));
        System.out.println("++++++++++++++++++++++++++");
        System.out.println(rid);
        System.out.println(pids);
        System.out.println("+++++++++++++++++++++++++");
        Integer integer = relationDao.addRolePermission(rid, pids);
        if(integer<=0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public List<String> getPermissionIds(String rid) {
        return relationDao.getPermissionIds(rid);
    }
}
